package com.jtpay.ysfproductionprocess.http.dataStructure;

import android.text.TextUtils;

import com.google.gson.Gson;
import com.jtpay.ysfproductionprocess.utils.LogUtil;
import com.jtpay.ysfproductionprocess.utils.PayDESUtil;
import okhttp3.MediaType;
import okhttp3.MultipartBody;
import okhttp3.RequestBody;

import java.io.File;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by Administrator on 2018/1/25.
 * JSON的装换类与RequestBody的包装类
 */

public class JsonParams extends HashMap<String, Object> {
    private String uploadFilePath;

    public void setUploadFilePath(String uploadFilePath) {
        this.uploadFilePath = uploadFilePath;
    }

    public RequestBody buildRequestBody() {
        return RequestBody.create(MediaType.parse("application/json; charset=utf-8"), new Gson().toJson(this));
    }

    public RequestBody buildMultipartBodyRequestBody() {
        if (TextUtils.isEmpty(uploadFilePath)) {
            return buildRequestBody();
        }
        File file = new File(uploadFilePath);
        RequestBody fileBody = RequestBody.create(MediaType.parse("application/octet-stream"), file);
        return new MultipartBody.Builder()
                .setType(MultipartBody.FORM)
                .addPart(fileBody)
                .build();
    }

    public String getJsonObject() {
        return new Gson().toJson(this);
    }

    @Override
    public String toString() {
        return "JsonParams: " + getJsonObject();
    }

    /**
     * @return des EncodeObject
     */
    public String getEncodeObject() {
        LogUtil.log(toString());
        return PayDESUtil.encode(getJsonObject()).replace("\"", "");
    }
}
